<?php
require_once(dirname(__FILE__)."/../lib/Loader.php");
cfgload("defines");
cload("Smarty.smartyRenderer");
cload("requestProcessor");
cload("user.usersList");
cload("user.user");

/**
 * Description of admUsersForm
 *
 * @author Di_Moon
 */
class admUsersForm extends smartyRenderer {
    protected $_dbh;
    protected $_id;
    protected $_action;
    
    function __construct($dbh) {
        parent::__construct();
        $this->_dbh = $dbh;
        
        $this->_id = requestProcessor::getVar("id", -1);
        $this->_action = requestProcessor::getVar("action");
    }
    
    function render() {
        $_user = new user($this->_dbh, $this->_id);
        
        if($this->_action=="form") {
            $post = requestProcessor::post();
            $post->del("action");
            
            if($post->get("id")==-1) {
                // Добавляем пользователя. Обнулим id
                $post->set("id", 0);
                $post->set("password", md5($post->get("password")));
            } else {
                // Если делаем обновление, то проверим поле пароля. Если оно пусто, то пароль не трогаем
                if($post->get("password")=="") {
                    $post->del("password");                
                } else {
                    $post->set("password", md5($post->get("password")));
                }
            }
            $_user->setElements($post);
            $_user->save();
            $_list = new usersList($this->_dbh);
            $_List = $_list->getList();
            $_user->setElements($_List[$post->get("name")]);
            $this->_id = $_user->get("id");
        }
        
        $formArr = array();
        $formArr["id"] = $this->_id;
        $formArr["name"] = htmlspecialchars($_user->get("name"));
        $formArr["mail"] = htmlspecialchars($_user->get("mail"));
        $formArr["enabled"] = $_user->get("enabled");
        
        $this->assign("userData", $formArr);
        return $this->fetch("admUsersForm.tpl");
    }
}

?>
